/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package at.socialconference.web.client.ui;

import at.socialconference.inter.connection.protocol.SocketProtocol;
import at.socialconference.inter.model.Login;
import at.socialconference.inter.model.User;
import at.socialconference.web.client.SCwebService;
import at.socialconference.web.client.controller.ViewController;
import at.socialconference.web.client.util.AsyncCallbackGetCountries;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Provides the registration form of the SCweb application
 * 
 */
public class Registration extends Composite implements ClickHandler {

	private TextBox txtBoxFirstName;
	private TextBox txtBoxLastName;
	private TextBox txtBoxDBLP;
	private TextBox txtBoxUsername;
	private PasswordTextBox pwTxtBox;
	private RadioButton radioButtonMale;
	private RadioButton radioButtonFemale;
	private ListBox listBox;
	private TextBox txtBoxPhone;
	private TextBox txtBoxTitle;
	private TextBox txtBoxWebpage;
	private TextBox txtBoxAddress;
	private TextBox txtBoxCity;
	private TextBox txtBoxZIP;
	private TextBox txtBoxMail;
	private Button btnSubmit;
	private Label lblFirstNameError = new Label("* missing or invalid");
	private Label lblLastNameError = new Label("* missing or invalid");
	private Label lblUsernameError = new Label("* missing or invalid");
	private Label lblPasswordError = new Label("* missing or invalid");

	private class AsyncCallbackRegistration implements AsyncCallback<Integer> {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Connection Problem");
		}

		@Override
		public void onSuccess(Integer result) {
			highlightInputMistake(result);
			switch (result) {
				case -1:
					Window.alert("Internal server error");
					break;
				case SocketProtocol.OK:
					Window.alert("Registration successful");
					ViewController.getInstance().showLoginForm();
					break;
				case SocketProtocol.RESP_DUPLOGIN:
					lblUsernameError.setText("* username already taken");
					break;
				case SocketProtocol.RESP_NOFIRSTNAME:
					lblUsernameError.setText("* missing or invalid");
				case SocketProtocol.RESP_NOLASTNAME:
				case SocketProtocol.RESP_NOUSERNAME:
				case SocketProtocol.RESP_NOPASSWORD:
					//DO NOTHING
					break;
				default:
					Window.alert("Internal erver error: Illegal status");
					break;
			}
		}
	}

	private void highlightInputMistake(int status) {
		lblFirstNameError.setVisible(false);
		txtBoxFirstName.getElement().getStyle().clearProperty("border-color");
		lblLastNameError.setVisible(false);
		txtBoxLastName.getElement().getStyle().clearProperty("border-color");
		lblUsernameError.setVisible(false);
		txtBoxUsername.getElement().getStyle().clearProperty("border-color");
		lblPasswordError.setVisible(false);
		pwTxtBox.getElement().getStyle().clearProperty("border-color");
		
		if (status == SocketProtocol.RESP_NOFIRSTNAME) {
			txtBoxFirstName.getElement().getStyle().setProperty("border-color", "red");
			lblFirstNameError.setVisible(true);
		} else if (status == SocketProtocol.RESP_NOLASTNAME) {
			txtBoxLastName.getElement().getStyle().setProperty("border-color", "red");
			lblLastNameError.setVisible(true);
		} else if (status == SocketProtocol.RESP_NOUSERNAME || status == SocketProtocol.RESP_DUPLOGIN) {
			txtBoxUsername.getElement().getStyle().setProperty("border-color", "red");
			lblUsernameError.setVisible(true);
		} else if (status == SocketProtocol.RESP_NOPASSWORD) {
			pwTxtBox.getElement().getStyle().setProperty("border-color", "red");
			lblPasswordError.setVisible(true);
		}
	}

	public Registration() {

		lblFirstNameError.setStyleName("lblError");
		lblLastNameError.setStyleName("lblError");
		lblUsernameError.setStyleName("lblError");
		lblPasswordError.setStyleName("lblError");
		FlexTable flexTable = new FlexTable();
		initWidget(flexTable);

		Label lblri = new Label("Required information:");
		lblri.setStyleName("textbig");

		flexTable.setWidget(0, 0, lblri);
		flexTable.getFlexCellFormatter().setColSpan(0, 0, 2);

		Label lblFirstName = new Label("First Name");
		flexTable.setWidget(1, 0, lblFirstName);
		txtBoxFirstName = new TextBox();
		HorizontalPanel horizontalPanelFirstName = new HorizontalPanel();
		flexTable.setWidget(1, 1, horizontalPanelFirstName);
		horizontalPanelFirstName.add(txtBoxFirstName);
		horizontalPanelFirstName.add(lblFirstNameError);


		Label lblLastName = new Label("Last Name");
		flexTable.setWidget(2, 0, lblLastName);
		txtBoxLastName = new TextBox();
		HorizontalPanel horizontalPanelLastName = new HorizontalPanel();
		flexTable.setWidget(2, 1, horizontalPanelLastName);
		horizontalPanelLastName.add(txtBoxLastName);
		horizontalPanelLastName.add(lblLastNameError);
		
		Label lblUsername = new Label("Username");
		flexTable.setWidget(3, 0, lblUsername);
		txtBoxUsername = new TextBox();
		HorizontalPanel horizontalPanelUsername = new HorizontalPanel();
		flexTable.setWidget(3, 1, horizontalPanelUsername);
		horizontalPanelUsername.add(txtBoxUsername);
		horizontalPanelUsername.add(lblUsernameError);

		Label lblPassword = new Label("Password");
		flexTable.setWidget(4, 0, lblPassword);
		pwTxtBox = new PasswordTextBox();
		pwTxtBox.setStyleName("gwt-TextBox");
		HorizontalPanel horizontalPanelPassword = new HorizontalPanel();
		flexTable.setWidget(4, 1, horizontalPanelPassword);
		horizontalPanelPassword.add(pwTxtBox);
		horizontalPanelPassword.add(lblPasswordError);

		Label lblGender = new Label("Gender");
		flexTable.setWidget(5, 0, lblGender);

		HorizontalPanel horizontalPanelGender = new HorizontalPanel();
		flexTable.setWidget(5, 1, horizontalPanelGender);

		radioButtonMale = new RadioButton("gender", "Male");
		radioButtonMale.setValue(true);
		horizontalPanelGender.add(radioButtonMale);
		horizontalPanelGender.setCellWidth(radioButtonMale, "110");
		radioButtonMale.setWidth("154px");

		radioButtonFemale = new RadioButton("gender", "Female");
		horizontalPanelGender.add(radioButtonFemale);

		Label lblCountry = new Label("Country");
		flexTable.setWidget(6, 0, lblCountry);

		listBox = new ListBox();
		flexTable.setWidget(6, 1, listBox);
		listBox.setVisibleItemCount(1);
		SCwebService.Util.getInstance().getCountries(new AsyncCallbackGetCountries(listBox));

		Label lbloi = new Label("Optional information:");
		lbloi.setStyleName("textbig");

		flexTable.setWidget(7, 0, lbloi);
		flexTable.getFlexCellFormatter().setColSpan(7, 0, 2);

		Label lblPhone = new Label("Phone");
		flexTable.setWidget(8, 0, lblPhone);

		txtBoxPhone = new TextBox();
		flexTable.setWidget(8, 1, txtBoxPhone);

		Label lblTitle = new Label("Title");
		flexTable.setWidget(9, 0, lblTitle);

		txtBoxTitle = new TextBox();
		flexTable.setWidget(9, 1, txtBoxTitle);

		Label lblWebpage = new Label("Webpage");
		flexTable.setWidget(10, 0, lblWebpage);

		txtBoxWebpage = new TextBox();
		flexTable.setWidget(10, 1, txtBoxWebpage);

		Label lblDBLP = new Label("DBLP Link");
		flexTable.setWidget(11, 0, lblDBLP);

		HorizontalPanel horizontalPanelDBLP = new HorizontalPanel();
		flexTable.setWidget(11, 1, horizontalPanelDBLP);
		txtBoxDBLP = new TextBox();
		horizontalPanelDBLP.add(txtBoxDBLP);
		Button btnDBLP = new Button("Get by Name");
		btnDBLP.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				showDBLPPopup();
			}
		});
		horizontalPanelDBLP.add(btnDBLP);

		Label lblAddress = new Label("Address");
		flexTable.setWidget(12, 0, lblAddress);

		txtBoxAddress = new TextBox();
		flexTable.setWidget(12, 1, txtBoxAddress);

		Label lblCity = new Label("City");
		flexTable.setWidget(13, 0, lblCity);

		txtBoxCity = new TextBox();
		flexTable.setWidget(13, 1, txtBoxCity);

		Label lblZIP = new Label("ZIP");
		flexTable.setWidget(14, 0, lblZIP);

		txtBoxZIP = new TextBox();
		flexTable.setWidget(14, 1, txtBoxZIP);

		Label lblMail = new Label("Mail");
		flexTable.setWidget(15, 0, lblMail);

		txtBoxMail = new TextBox();
		flexTable.setWidget(15, 1, txtBoxMail);

		btnSubmit = new Button("Submit");
		btnSubmit.addClickHandler(this);

		flexTable.setWidget(19, 1, btnSubmit);
		
		//set all error labels to invisbile
		highlightInputMistake(-1);
	}

	private void showDBLPPopup() {
		new DBLPPopup(txtBoxDBLP, txtBoxFirstName.getText(), txtBoxLastName.getText()).show();
	}

	public void onClick(ClickEvent event) {
		if (event.getSource() != null) {
			if (event.getSource().equals(btnSubmit)) {
				// construct Login object
				Login login = new Login();
				login.setUser(txtBoxUsername.getText());
				login.setPwd(pwTxtBox.getText());

				// construct User object
				User user = new User();
				user.setFirstname(txtBoxFirstName.getText());
				user.setLastname(txtBoxLastName.getText());
				user.setDblp(txtBoxDBLP.getText());
				user.setGender(radioButtonMale.getValue() ? "m" : "f");
				user.setCountry(listBox.getValue(listBox.getSelectedIndex()));
				user.setPhone(txtBoxPhone.getText());
				user.setTitle(txtBoxTitle.getText());
				user.setWebpage(txtBoxWebpage.getText());
				user.setAddress(txtBoxAddress.getText());
				user.setCity(txtBoxCity.getText());
				user.setZip(txtBoxZIP.getText());
				user.setMail(txtBoxMail.getText());

				SCwebService.Util.getInstance().register(user, login, new AsyncCallbackRegistration());
			}
		}

	}

}
